﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LOMBARDISPICK.Domain.Services;
using LOMBARDISPICK.Model;
using LOMBARDISPICK.Domain.Infrastructure;

namespace LOMBARDISPICK.Web.Controllers
{
	public class BaseController<T, U> : Controller
		where T : class
		where U : IService<T>
	{
		//protected override void OnException(ExceptionContext filterContext)
		//{

		//    var appErrorLogservice = DependencyResolver.Current.GetService<IAPP_ERROR_LOGService>();
		//    var employeePreferenceService = DependencyResolver.Current.GetService<IEMPLOYEE_PREFERENCESService>();
		//    EMPLOYEE_PREFERENCES currentEmployeePref = employeePreferenceService.getCurrentEmployeePreferences();

		//    var model = new APP_ERROR_LOG{
		//        EMPLOYEE_ID = currentEmployeePref.EMPLOYEE_ID,
		//        APP_USER_ROLE_ID = currentEmployeePref.APP_USER_ROLE_ID,
		//        VERSION_AND_USER_ROLE_CONTROL_ID= currentEmployeePref.VERSION_AND_USER_ROLE_CONTROL_ID,
		//        TIME=  DateTime.Now,
		//        MESSAGE = filterContext.Exception.Message.Substring(0, Math.Min(filterContext.Exception.Message.Length, 499)),
		//        STACKTRACE = filterContext.Exception.StackTrace.Substring(0, Math.Min(filterContext.Exception.StackTrace.Length, 999)),
		//        ACTION_NAME = filterContext.RouteData.Values.Values.Last().ToString(),
		//        CONTROLLER_NAME = filterContext.RouteData.Values.Values.First().ToString()
		//    };

		//    appErrorLogservice.create(model);
		//}

		protected ActionResult returnToIndex(int id)
		{
			//can only return a partial view from an ajax form which these all are, but the case has been left to redirect to the index for a non ajax form.
			if (Request.IsAjaxRequest())
			{
				//if an ajax form from a workflow page then redirect to workflow success page.
				if (Request.UrlReferrer.ToString().Contains("WORKFLOW_") || Request.UrlReferrer.ToString().ToUpper().Contains("/HOME/"))
				{
					//redirect to success message which closes page
					return RedirectToAction("Success", "DIALOG_WINDOW_SUBMISSION", new { PRIMARY_KEY = id });
				}
				else
				{
					return JavaScript("window.top.location.href ='" + Url.Action("Index") + "';");
				}
			}
			else
			{
				return RedirectToAction("Index");
			}
		}

		protected ActionResult returnToIndexWithParameter(int id, int id2)
		{
			//can only return a partial view from an ajax form which these all are, but the case has been left to redirect to the index for a non ajax form.
			if (Request.IsAjaxRequest())
			{
				//if an ajax form from a workflow page then redirect to workflow success page.
				if (Request.UrlReferrer.ToString().Contains("WORKFLOW_") || Request.UrlReferrer.ToString().ToUpper().Contains("/HOME/"))
				{
					//redirect to success message which closes page
					return RedirectToAction("Success", "DIALOG_WINDOW_SUBMISSION", new { PRIMARY_KEY = id });
				}
				else
				{
					return JavaScript("window.top.location.href ='" + Url.Action("Index", new { id = id2 }) + "';");
				}
			}
			else
			{
				return RedirectToAction("Index", new { id = id2 });
			}
		}

		protected ActionResult returnPartialView(IEnumerable<T> ServiceInst)
		{
			if (Request.IsAjaxRequest())
			{
				return PartialView(ServiceInst);
			}
			return View(ServiceInst);
		}

		protected ActionResult returnPartialView(T ServiceInst)
		{
			if (Request.IsAjaxRequest())
			{
				return PartialView(ServiceInst);
			}
			return View(ServiceInst);
		}

		protected ActionResult returnPartialView()
		{
			if (Request.IsAjaxRequest())
			{
				return PartialView();
			}
			return View();
		}
	}


	public class BaseController : Controller
	{
		//protected override void OnException(ExceptionContext filterContext)
		//{

		//    var appErrorLogservice = DependencyResolver.Current.GetService<IAPP_ERROR_LOGService>();
		//    var employeePreferenceService = DependencyResolver.Current.GetService<IEMPLOYEE_PREFERENCESService>();
		//    EMPLOYEE_PREFERENCES currentEmployeePref = employeePreferenceService.getCurrentEmployeePreferences();

		//    var model = new APP_ERROR_LOG
		//    {
		//        EMPLOYEE_ID = currentEmployeePref.EMPLOYEE_ID,
		//        APP_USER_ROLE_ID = currentEmployeePref.APP_USER_ROLE_ID,
		//        VERSION_AND_USER_ROLE_CONTROL_ID = currentEmployeePref.VERSION_AND_USER_ROLE_CONTROL_ID,
		//        TIME = DateTime.Now,
		//        MESSAGE = filterContext.Exception.Message.Substring(0, Math.Min(filterContext.Exception.Message.Length, 499)),
		//        STACKTRACE = filterContext.Exception.StackTrace.Substring(0, Math.Min(filterContext.Exception.StackTrace.Length, 999)),
		//        ACTION_NAME = filterContext.RouteData.Values.Values.Last().ToString(),
		//        CONTROLLER_NAME = filterContext.RouteData.Values.Values.First().ToString()
		//    };

		//    appErrorLogservice.create(model);
		//}
	}
}
